/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccOthersSb;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccOthersSbRepository;
import me.zhengjie.modules.system.service.DccOthersSbService;
import me.zhengjie.modules.system.service.dto.DccOthersSbDto;
import me.zhengjie.modules.system.service.dto.DccOthersSbQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccOthersSbMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-06-12
**/
@Service
@RequiredArgsConstructor
public class DccOthersSbServiceImpl implements DccOthersSbService {

    private final DccOthersSbRepository dccOthersSbRepository;
    private final DccOthersSbMapper dccOthersSbMapper;

    @Override
    public Map<String,Object> queryAll(DccOthersSbQueryCriteria criteria, Pageable pageable){
        Page<DccOthersSb> page = dccOthersSbRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccOthersSbMapper::toDto));
    }

    @Override
    public List<DccOthersSbDto> queryAll(DccOthersSbQueryCriteria criteria){
        return dccOthersSbMapper.toDto(dccOthersSbRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccOthersSbDto findById(Integer id) {
        DccOthersSb dccOthersSb = dccOthersSbRepository.findById(id).orElseGet(DccOthersSb::new);
        ValidationUtil.isNull(dccOthersSb.getId(),"DccOthersSb","id",id);
        return dccOthersSbMapper.toDto(dccOthersSb);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccOthersSbDto create(DccOthersSb resources) {
        return dccOthersSbMapper.toDto(dccOthersSbRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccOthersSb resources) {
        DccOthersSb dccOthersSb = dccOthersSbRepository.findById(resources.getId()).orElseGet(DccOthersSb::new);
        ValidationUtil.isNull( dccOthersSb.getId(),"DccOthersSb","id",resources.getId());
        dccOthersSb.copy(resources);
        dccOthersSbRepository.save(dccOthersSb);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccOthersSbRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccOthersSbDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccOthersSbDto dccOthersSb : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类型", dccOthersSb.getType());
            map.put("内部编码", dccOthersSb.getInner());
            map.put("国药准字", dccOthersSb.getGuoyao());
            map.put("名称", dccOthersSb.getName());
            map.put("商品", dccOthersSb.getShangping());
            map.put("规格", dccOthersSb.getGuige());
            map.put("剂型", dccOthersSb.getJixing());
            map.put("单位", dccOthersSb.getDanwei());
            map.put("生产厂家", dccOthersSb.getProduct());
            map.put("附加", dccOthersSb.getOthers());
            map.put("新增时间", dccOthersSb.getCreatedAt());
            map.put("修改时间", dccOthersSb.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}